import { message } from 'antd';
import {
  reqGetStatusDataRead,
  reqOpenOrRealDataRead,
  reqGetOilConsume,
  reqHistoricalPositionByDate,
} from './service';
export default {
  namespace: 'dataforCars',
  state: {
    vehicleInfo: {},
    status: false,
    postionHsData: [],
    speedData: [],
    oilSumnData: [],
  },
  reducers: {
    setVehicle(state, { payload }) {
      return { ...state, vehicleInfo: payload };
    },
    setStatus(state, { payload }) {
      return { ...state, status: payload };
    },
    setHsPosition(state, { payload }) {
      return { ...state, postionHsData: payload };
    },
    setSpeed(state, { payload }) {
      return { ...state, speedData: payload };
    },
    setOilSum(state, { payload }) {
      return { ...state, oilSumnData: payload };
    },
  },
  effects: {
    *fetchOne({ payload }, { call, put }) {
      const res = yield call(reqOneVehicle, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'setVehicle',
            payload: res.data,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqHistoricalPositionByDate({ payload }, { call, put }) {
      //获取历史位置信息
      const res = yield call(reqHistoricalPositionByDate, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'setHsPosition',
            payload: res.data,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
    *reqGetOilConsume({ payload }, { call, put }) {
      //获取历史位置信息
      const res = yield call(reqHistoricalPositionByDate, payload);
      try {
        if (res.code === 0) {
          yield put({
            type: 'setOilSum',
            payload: res.data,
          });
        }
      } catch (error) {
        message.error(error);
      }
    },
  },
};
